﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace SiparisMail
{
    public class Account
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
    }
    public class eMail
    {
        Stack<Account> _hesaplar = new Stack<Account>();
        private static eMail _email;
        private eMail()
        {
            HesapDoldur();
        }

        private void HesapDoldur()
        {
            string[] emailuser = Config.GetConfig.GetValue(Parametre.mailUser).Split(',');
            string[] emailpwd = Config.GetConfig.GetValue(Parametre.emailPwd).Split(',');
            string[] kimden = Config.GetConfig.GetValue(Parametre.email).Split(',');
            for (int i = 0; i < emailuser.Length; i++)
            {
                _hesaplar.Push(new Account()
                {
                    Email = kimden[i],
                    Password = emailpwd[i],
                    UserName = emailuser[i]
                });
            }
        }
        public static eMail Get
        {
            get
            {
                if (_email == null)
                {
                    _email = new eMail();
                }
                return _email;
            }

        }

        private bool Gmail(string kimden, string kime, string kullaniciAdi, string sifre, string konu, string icerik, string sunucuAdi)
        {
            try
            {
                System.Web.Mail.MailMessage objMail = new System.Web.Mail.MailMessage();
                objMail.To = kime;
                objMail.From = "\\" + kimden + "\\ <" + kimden + ">";
                objMail.Subject = konu;
                objMail.Body = icerik;
                objMail.BodyFormat = System.Web.Mail.MailFormat.Html;
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtsperver", sunucuAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", kullaniciAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sifre);
                System.Web.Mail.SmtpMail.SmtpServer = sunucuAdi;
                System.Web.Mail.SmtpMail.Send(objMail);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(kime + "   Adlı kullanıcıya mail iletilemedi lütfen tekrar deneyin \r\n" + ex.Message);
                return false;
            }
        }
        private bool Gmail2(string kimden, string kime, string kullaniciAdi, string sifre, string konu, string icerik, string sunucuAdi, string atach_file)
        {
            try
            {
                System.Web.Mail.MailMessage objMail = new System.Web.Mail.MailMessage();
                objMail.To = kime;
                objMail.From = "\\" + kimden + "\\ <" + kimden + ">";
                objMail.Subject = konu;
                objMail.Body = icerik;
                objMail.BodyFormat = System.Web.Mail.MailFormat.Html;
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtsperver", sunucuAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", kullaniciAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sifre);
                if (!string.IsNullOrEmpty(atach_file))
                {
                    objMail.Attachments.Add(new System.Web.Mail.MailAttachment(atach_file));
                }
                System.Web.Mail.SmtpMail.SmtpServer = sunucuAdi;
                System.Web.Mail.SmtpMail.Send(objMail);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private bool Smtp(string kimden, string kime, string kullaniciAdi, string sifre, string konu, string icerik, string sunucuAdi, string atach_file)
        {
            try
            {
                System.Web.Mail.MailMessage objMail = new System.Web.Mail.MailMessage();
                objMail.To = kime;
                objMail.From = "\\" + kimden + "\\ <" + kimden + ">";
                objMail.Subject = konu;
                objMail.Body = icerik;
                objMail.BodyFormat = System.Web.Mail.MailFormat.Html;
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtsperver", sunucuAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "587");
                //objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true")
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", kullaniciAdi);
                objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", sifre);
                if (!string.IsNullOrEmpty(atach_file))
                {
                    objMail.Attachments.Add(new System.Web.Mail.MailAttachment(atach_file));
                }
                System.Web.Mail.SmtpMail.SmtpServer = sunucuAdi;
                System.Web.Mail.SmtpMail.Send(objMail);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool MailGonder(string kime, string konu, string icerik, string atach_file, int depoNo)
        {
            Account act = _hesaplar.Pop();
            if (act == null)
            {
                HesapDoldur();
                MailGonder(kime, konu, icerik, atach_file, depoNo);
            }

            string emailuser = act.UserName;
            string emailpwd = act.Password;
            string kimden = act.Email;
            string sunucu = Config.GetConfig.GetValue(Parametre.smtpServer);


            if (Config.GetConfig.GetValue(Parametre.ServerType) == "Gmail")
            {
                if (eMail.Get.Gmail2(kimden, kime, emailuser, emailpwd, konu, icerik, sunucu, atach_file))
                {
                    string icmail = DepoBazliGonder(depoNo);
                    if (!string.IsNullOrEmpty(icmail))
                        eMail.Get.Gmail2(kimden, icmail, emailuser, emailpwd, konu, icerik, sunucu, atach_file);

                    return true;
                }
            }
            else
            {
                if (eMail.Get.Smtp(kimden, kime, emailuser, emailpwd, konu, icerik, sunucu, atach_file))
                {
                    string icmail = DepoBazliGonder(depoNo);
                    if (!string.IsNullOrEmpty(icmail))
                        eMail.Get.Gmail2(kimden, icmail, emailuser, emailpwd, konu, icerik, sunucu, atach_file);

                    return true;
                }
            }
            return false;
        }

        private string DepoBazliGonder(int depoNo)
        {
            string[] param = Config.GetConfig.GetValue(Parametre.DepoBazliEmail).Split(',');
            foreach (string item in param)
            {
                if (item.Split('-')[0] == depoNo.ToString())
                {
                    return item.Split('-')[1];
                }
            }
            return "";
        }
    }
}
